package com.feiluan.channel;


import com.feiluan.adapter.HttpRequestHandler;
import com.feiluan.config.NettyServerConfig;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Demo class
 *
 * @author keriezhang
 * @date 2016/10/31
 */
@Component
public class NioWebSocketChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Autowired
    NettyServerConfig nettyConfig;


    @Resource
    private HttpRequestHandler httpRequestHandler;

    @Override
    protected void initChannel(SocketChannel ch) {

        //设置log监听器，并且日志级别为debug，方便观察运行流程
        ch.pipeline().addLast("logging", new LoggingHandler("DEBUG"));
        //设置解码器
        ch.pipeline().addLast("http", new HttpServerCodec());
        //聚合器，使用websocket会用到
        ch.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
        //用于大数据的分区传输
        ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
        //自定义的业务handler
        ch.pipeline().addLast("handler", httpRequestHandler);

    }
}
